package com.light.common;

import com.jfinal.aop.Duang;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.render.FreeMarkerRender;
import com.jfinal.render.ViewType;
import com.jfinal.template.Engine;
import com.light.modules.admin.common.handler.ZcurdHandler;
import com.light.modules.admin.system.service.TaskService;

import freemarker.template.TemplateModelException;
import org.apache.log4j.Logger;

/**
 * API引导式配置
 */
public class ZcurdConfig extends JFinalConfig {

    private static final Logger LOGGER = Logger.getLogger(ZcurdConfig.class);

    /**
     * 配置常量
     */
    @Override
    public void configConstant(Constants me) {
        // 加载少量必要配置，随后可用PropKit.get(...)获取值
        PropKit.use("config.properties");
        me.setDevMode(PropKit.getBoolean("devMode", false));
        me.setViewType(ViewType.FREE_MARKER);
    }

    /**
     * 配置路由
     */
    @Override
    public void configRoute(Routes me) {
        me.add(new AdminRoutes());
        me.add(new WebRoutes());
        me.add(new AppRoutes());
    }

    /**
     * 配置插件
     */
    @Override
    public void configPlugin(Plugins me) {
        // 系统数据库
        DruidPlugin sysDruidPlugin = new DruidPlugin(PropKit.get("base_jdbcUrl"), PropKit.get("base_user"), PropKit.get("base_password").trim());
        me.add(sysDruidPlugin);
        ActiveRecordPlugin arp = new ActiveRecordPlugin("zcurd_base", sysDruidPlugin);
        arp.setShowSql(true);
        me.add(arp);
        _MappingKit.sysMapping(arp);

        //业务数据库
        DruidPlugin busiDruidPlugin = new DruidPlugin(PropKit.get("busi_jdbcUrl"), PropKit.get("busi_user"), PropKit.get("busi_password").trim());
        me.add(busiDruidPlugin);
        ActiveRecordPlugin arpAir = new ActiveRecordPlugin("zcurd_busi", busiDruidPlugin);
        arpAir.setShowSql(true);
        me.add(arpAir);
        _MappingKit.busiMapping(arpAir);
    }

    /**
     * 配置全局拦截器
     */
    @Override
    public void configInterceptor(Interceptors me) {
       /* me.add(new SessionInViewInterceptor());
        me.add(new AuthInterceptor());
        me.add(new ErrorInterceptor());*/
    }

    /**
     * 配置处理器
     */
    @Override
    public void configHandler(Handlers me) {
        me.add(new ZcurdHandler());
    }

    @Override
    public void afterJFinalStart() {
        try {
            FreeMarkerRender.getConfiguration().setSharedVariable("basePath", JFinal.me().getContextPath());
        } catch (TemplateModelException e) {
            LOGGER.error("ZcurdConfig>>afterJFinalStart:" + e.getLocalizedMessage());
        }

        //定时任务
        TaskService taskService = Duang.duang(TaskService.class);
        taskService.startAll();
    }


    @Override
    public void configEngine(Engine me) {

    }
}
